package com.xinsmartsky.penmaji.penmaji;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.xinsmartsky.penmaji.NewGridView.NewGridView;
import com.xinsmartsky.penmaji.SerialPort.FileOperation;
import com.xinsmartsky.penmaji.model.MemRegionModel;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EnglishAndDigitActivity extends Activity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    private Button deglish_btn_back;
    private Button deglish_btn_jsq;//计数器
    private int devfd, chinesenum, diff;//串口号 字条号 差值
    private SerialPortActivity serialport = new SerialPortActivity();
    private TextView dieg_tv_01;
    private int[] addrarray;
    private byte[] valuearray;
    private EditText digit_edt_message01;
    private EditText digit_edt_message02;
    private EditText digit_edt_message03;
    private EditText digit_edt_message04;
    private EditText deglish_edt_width;//宽度
    private EditText deglish_edt_zfgd;//高度
    private EditText deglish_edt_zfwz;//字条位移
    private EditText deglish_edt_zfjc;//加粗
    private EditText deglish_edt_xzzx;//字型
    private EditText deglish_edt_zfjg;//间隔
    private MemRegionModel mem = new MemRegionModel();
    private EditText demenu_ulimit;//上限值
    private EditText demenu_llimit;//下限值
    private EditText demenu_digit;//位数
    private final byte AsciiDiff = 48;//将数据转换为ascii码值 写入数据块中
    SerialPortActivity start_serialport = new SerialPortActivity();
    FileOperation fileOperation = new FileOperation();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_digit_eglish);
        devfd = getIntent().getIntExtra("devfd", 0);
        chinesenum = getIntent().getIntExtra("num", 0);
        FindViewById();
        setOnClickListener();
        init();
    }

    /*将值初始化*/
    private void init(){
        diff = 256 * (chinesenum - 1);
        //设置参数 的 地址数组
        addrarray = new int[mem.EAD_AddrArray.length];
        for(int i = 0; i < addrarray.length; i++){
            addrarray[i] = (mem.EAD_AddrArray[i]  + diff);
        }
        //值数组；ASCII码值0 ---> 48
//        valuearray = new byte[]{48, 49, 48, 49, 57, 48, 49, 48, 49, 49, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 49,'N' ,'P', 'N'};
//        Log.i("sss", "   字条号 = " + chinesenum + " mem,,," + mem.Chinese_16);
    }


    private void FindViewById() {
        deglish_edt_width = (EditText)findViewById(R.id.deglish_edt_width);
        deglish_edt_zfgd = (EditText)findViewById(R.id.deglish_edt_zfgd);
        deglish_edt_zfwz = (EditText)findViewById(R.id.deglish_edt_zfwz);
        deglish_edt_zfjc = (EditText)findViewById(R.id.deglish_edt_zfjc);
        deglish_edt_xzzx = (EditText)findViewById(R.id.deglish_edt_xzzx);
        deglish_edt_zfjg = (EditText)findViewById(R.id.deglish_edt_zfjg);

        ((Button)findViewById(R.id.deglish_btn_write)).setOnClickListener(this);//向文件中写入数据
        ((Button)findViewById(R.id.deglish_btn_send)).setOnClickListener(this);//发送数据
        ((ToggleButton)findViewById(R.id.deglish_tb_zfjc)).setOnCheckedChangeListener(this);//加粗
        ((ToggleButton)findViewById(R.id.deglish_tb_zfdz)).setOnCheckedChangeListener(this);//字符倒字
        ((ToggleButton)findViewById(R.id.deglish_tb_zffz)).setOnCheckedChangeListener(this);//字符反字
        digit_edt_message01 = (EditText)findViewById(R.id.digit_edt_message01);
        digit_edt_message02 = (EditText)findViewById(R.id.digit_edt_message02);
        digit_edt_message03 = (EditText)findViewById(R.id.digit_edt_message03);
        digit_edt_message04 = (EditText)findViewById(R.id.digit_edt_message04);
        dieg_tv_01 = (TextView) findViewById(R.id.dieg_tv_01);
        dieg_tv_01.requestFocus();
        deglish_btn_back = (Button) findViewById(R.id.deglish_btn_back);
        deglish_btn_jsq = (Button) findViewById(R.id.deglish_btn_jsq);
        deglish_rela_root = findViewById(R.id.deglish_rela_root);
    }

    private void setOnClickListener() {
        deglish_btn_back.setOnClickListener(this);
        deglish_btn_jsq.setOnClickListener(this);
    }

    //去除字符串中的空格、回车、换行符、制表符
    private String replaceBlank(String str) {
        String dest = "";
        if (str!=null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }

    private byte[]ArrayC = new byte[120];//存放内容的数组
    private byte[][]message = new byte[4][30];
    //将编辑的内容写入文件
    private void setEAndDValues(){
        message[0] = digit_edt_message01.getText().toString().getBytes();//空格
        message[1] = digit_edt_message02.getText().toString().getBytes();
        message[2] = digit_edt_message03.getText().toString().getBytes();
        message[3] = digit_edt_message04.getText().toString().getBytes();
        for(int j = 0; j < 4; j++){
            if(message[j].length > 30){//包括空格
                Toast.makeText(EnglishAndDigitActivity.this, "编辑的信息过长", 3000).show();
                break;
            }else {
                for(int i = 0; i < 30; i++){
                    ArrayC[j * 30 + i] = message[j][i];
                }
            }
        }
        //将byte数组转换为字符串 trim去掉首尾空格
        String str = new String(ArrayC);
        str = replaceBlank(str);
        if(str.length() != str.getBytes().length){
            Toast.makeText(EnglishAndDigitActivity.this, "禁止输入汉字", 3000).show();
        }else {
            fileOperation.SetArrayToAddress(4, chinesenum, ArrayC, -1);//向文件中写入信息
        }
    }
    //将设置的参数写入文件
    private void setParam(){
        //宽度
        valuearray[0] = (byte) ((Integer.valueOf(deglish_edt_width.getText().toString())) / 10);
        valuearray[1] = (byte) ((Integer.valueOf(deglish_edt_width.getText().toString())) % 10);
        //高度
        valuearray[4] = (byte) ((Integer.valueOf(deglish_edt_zfgd.getText().toString())) / 1);
        //高度位移
        valuearray[5] = (byte) ((Integer.valueOf(deglish_edt_zfwz.getText().toString())) / 1);
        //加重参数
        valuearray[6] = (byte) ((Integer.valueOf(deglish_edt_zfjc.getText().toString())) / 1);
        //字型
        valuearray[7] = (byte) ((Integer.valueOf(deglish_edt_xzzx.getText().toString())) / 10);
        valuearray[8] = (byte) ((Integer.valueOf(deglish_edt_xzzx.getText().toString())) % 10);
        //字符间隔
        valuearray[9] = (byte) ((Integer.valueOf(deglish_edt_zfjg.getText().toString())) / 1);
        //上限值
        valuearray[10] = (byte) ((Integer.valueOf(demenu_ulimit.getText().toString())) / 100000);
        valuearray[11] = (byte) ((Integer.valueOf(demenu_ulimit.getText().toString())) / 10000 % 10);
        valuearray[12] = (byte) ((Integer.valueOf(demenu_ulimit.getText().toString())) / 1000 % 10);
        valuearray[13] = (byte) ((Integer.valueOf(demenu_ulimit.getText().toString())) / 100 % 10);
        valuearray[14] = (byte) ((Integer.valueOf(demenu_ulimit.getText().toString())) / 10 % 10);
        valuearray[15] = (byte) ((Integer.valueOf(demenu_ulimit.getText().toString()))  % 10);
        //下限值
        valuearray[16] = (byte) ((Integer.valueOf(demenu_llimit.getText().toString())) / 100000);
        valuearray[17] = (byte) ((Integer.valueOf(demenu_llimit.getText().toString())) / 10000 % 10);
        valuearray[18] = (byte) ((Integer.valueOf(demenu_llimit.getText().toString())) / 1000 % 10);
        valuearray[19] = (byte) ((Integer.valueOf(demenu_llimit.getText().toString())) / 100 % 10);
        valuearray[20] = (byte) ((Integer.valueOf(demenu_llimit.getText().toString())) / 10 % 10);
        valuearray[21] = (byte) ((Integer.valueOf(demenu_llimit.getText().toString()))  % 10);
        //计数器长度
        valuearray[22] = (byte) ((Integer.valueOf(demenu_llimit.getText().toString()))  / 1);
        //转换为ASCII码
        for(int i = 0; i < 23; i++){
            valuearray[i] += AsciiDiff;
        }
        //向文件中写入信息
        fileOperation.setDataBlockValues(addrarray, valuearray);
    }


    private PopupWindow popupWindow;
    private View deglish_rela_root;
    //弹出pop界面
    //初始化
    private void PopupWindow() {
        View contentView = getLayoutInflater().inflate(R.layout.activity_digit_english_menu, null);//null??
        RelativeLayout deglish_rela_menu = (RelativeLayout) contentView.findViewById(R.id.deglish_rela_menu);
        Button demenu_btn_confirm = (Button)contentView.findViewById(R.id.demenu_btn_confirm);
        Button demenu_btn_cancel = (Button)contentView.findViewById(R.id.demenu_btn_cancel);
        demenu_ulimit = (EditText)contentView.findViewById(R.id.demenu_ulimit);
        demenu_llimit = (EditText)contentView.findViewById(R.id.demenu_llimit);
        demenu_digit = (EditText)contentView.findViewById(R.id.demenu_digit);
        demenu_btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deglish_btn_jsq.setText(demenu_ulimit.getText() + " / " + demenu_llimit.getText() + "   " +demenu_digit.getText());
                if (popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
            }
        });
         /*"取消"：消失*/
        demenu_btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
            }
        });
        popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(false);
        popupWindow.setAnimationStyle(R.style.popupanimation);
        //show
        popupWindow.showAtLocation(deglish_rela_root, Gravity.BOTTOM, 0, 0);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.deglish_btn_back: {
                this.finish();
                break;
            }

            case  R.id.deglish_btn_write:{
                setEAndDValues();
                setParam();
                break;
            }
            case  R.id.deglish_btn_send:{
                break;
            }
            //计数器设置
            case R.id.deglish_btn_jsq:{
                PopupWindow();
                break;
            }
        }


    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()){
            case R.id.deglish_tb_zfjc:{
                if(isChecked){
                    valuearray[23] = 'Y';
                    Log.i("sss", "加粗======" + valuearray[7]);
                }else {
                    valuearray[23] = 'N';
                }
                break;
            }
            case R.id.deglish_tb_zffz:{//反字
                if(isChecked){
                    valuearray[24] = 'N';
                }else {
                    valuearray[24] = 'P';
                }
                break;
            }
            case R.id.deglish_tb_zfdz:{//倒字
                if(isChecked){
                    valuearray[25] = 'Y';
                }else {
                    valuearray[25] = 'N';
                }
                break;
            }
        }
    }
}
